package com.sokal.easycare.modules.position.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.position.entity.ServiceOffer;
import com.sokal.easycare.modules.position.service.ServiceOfferService;

/**
 * 岗位职责Controller
 * @author yangmin
 * @version 2017-04-05
 */
@Controller
@RequestMapping(value = "${adminPath}/position/serviceOffer")
public class ServiceOfferController extends BaseController {

	@Autowired
	private ServiceOfferService serviceOfferService;
	
	@ModelAttribute
	public ServiceOffer get(@RequestParam(required=false) String id) {
		ServiceOffer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = serviceOfferService.get(id);
		}
		if (entity == null){
			entity = new ServiceOffer();
		}
		return entity;
	}
	
	@RequiresPermissions("position:serviceOffer:view")
	@RequestMapping(value = {"list", ""})
	public String list(ServiceOffer serviceOffer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ServiceOffer> page = serviceOfferService.findPage(new Page<ServiceOffer>(request, response), serviceOffer); 
		model.addAttribute("page", page);
		return "modules/position/serviceOfferList";
	}

	@RequiresPermissions("position:serviceOffer:view")
	@RequestMapping(value = "form")
	public String form(ServiceOffer serviceOffer, Model model) {
		model.addAttribute("serviceOffer", serviceOffer);
		return "modules/position/serviceOfferForm";
	}

	@RequiresPermissions("position:serviceOffer:edit")
	@RequestMapping(value = "save")
	public String save(ServiceOffer serviceOffer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, serviceOffer)){
			return form(serviceOffer, model);
		}
		serviceOfferService.save(serviceOffer);
		addMessage(redirectAttributes, "保存岗位职责成功");
		return "redirect:"+Global.getAdminPath()+"/position/serviceOffer/?repage";
	}
	
	@RequiresPermissions("position:serviceOffer:edit")
	@RequestMapping(value = "delete")
	public String delete(ServiceOffer serviceOffer, RedirectAttributes redirectAttributes) {
		serviceOfferService.delete(serviceOffer);
		addMessage(redirectAttributes, "删除岗位职责成功");
		return "redirect:"+Global.getAdminPath()+"/position/serviceOffer/?repage";
	}

}